package com.yogee.topseller.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.base.HttpFragment1;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AnimalUtil;
import com.yogee.topseller.MainActivity;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.SearchByConditionsActivity;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.adapter.HProductListAdapter;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.fragment.product.model.bean.ProductBean;
import com.yogee.topseller.fragment.product.model.bean.ProductListBean;
import com.yogee.topseller.fragment.product.model.bean.ProductTypeBean;
import com.yogee.topseller.fragment.product.model.bean.ProductTypeListBean;
import com.yogee.topseller.fragment.product.presenter.ProductPresenter;
import com.yogee.topseller.fragment.product.view.IproductView;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 产品列表
 *
 * @author hanyu
 *         created by 2017/6/30 16:23
 */

public class HomeProductFragment extends HttpFragment1 implements IproductView, RefreshUtil.OnRefreshListener {

    Unbinder unbinder;
    @BindView(R.id.rb_big_img)
    RadioButton rbBigImg;
    @BindView(R.id.rb_list)
    RadioButton rbList;
    @BindView(R.id.layout_tuijian)
    TextView layoutTuijian;
    @BindView(R.id.rv_product_list)
    EmptyRecyclerView rvProductList;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.refreshLayout1)
    TwinklingRefreshLayout refreshLayout1;
    @BindView(R.id.layout_qitou_icon)
    ImageView layoutQitouIcon;
    @BindView(R.id.layout_qixian_icon)
    ImageView layoutQixianIcon;
    @BindView(R.id.layout_shouyi_icon)
    ImageView layoutShouyiIcon;
    @BindView(R.id.layout_yongjin_icon)
    ImageView layoutYongjinIcon;

    int flag1, flag2, flag3, flag4, flag5;//0.未选中，1.选中降序，2.选中升序
    ProductPresenter mPresenter;            //presenter
    HProductListAdapter mAdapter;           //产品列表适配器
    List<ProductBean> dataList;            //产品列表
    int typeImgList = 1, sort = 0, type = 0;//typeImgList0.大图，typeImgList1.列表 ; sort排序字段0：推荐、1：起投、2：期限、3：佣金;  type0:正序    1:倒序
    ProductTypeBean titleBean;            //标题
    int page = 0, pageSize = 10;           //page页码，pageSize每页条数
    boolean refreshFlag = true;
    @BindView(R.id.text_qitou)
    TextView textQitou;
    @BindView(R.id.text_qixian)
    TextView textQixian;
    @BindView(R.id.text_shouyi)
    TextView textShouyi;
    @BindView(R.id.text_yongjin)
    TextView textYongjin;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    Unbinder unbinder1;

    @Override
    protected int getLayoutId() {
        return R.layout.home_product_fragment;
    }

    @Override
    protected void initView() {
        dataList = new ArrayList<>();
        mPresenter = new ProductPresenter(this);
        //获取标题，从上页传过来
        if (null != getArguments()) {
            titleBean = (ProductTypeBean) getArguments().getSerializable("titleBean");
            mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), 0, 10, sort, type);
        } else {
            mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", 0, 10, sort, type);
        }
        //默认推荐设置为true
        flag1 = 1;
        MySinaRefreshView headerView = new MySinaRefreshView(getActivity());
        refreshLayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        refreshLayout.setHeaderView(headerView);
        refreshLayout.setBottomView(new RefreshView(getActivity()));
        refreshLayout.setBottomHeight(50);

        MySinaRefreshView headerView1 = new MySinaRefreshView(getActivity());
        refreshLayout1.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        refreshLayout1.setHeaderView(headerView1);
        refreshLayout1.setBottomView(new RefreshView(getActivity()));
        refreshLayout1.setBottomHeight(50);
        mAdapter = new HProductListAdapter(getActivity());
        rvProductList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvProductList.setHasFixedSize(true);
        rvProductList.setAdapter(mAdapter);
        rvProductList.setEmptyView(refreshLayout1);
    }

    /**
     * 获取产品列表成功回调
     */
    @Override
    public void loadProductSuccess(ProductListBean bean) {
        MySelfInfo.getInstance().setIsPay(bean.getIspay());
        if (null != bean.getProductList() && bean.getProductList().size() > 0) {
            if (refreshFlag) {
                dataList = bean.getProductList();
                rvProductList.smoothScrollToPosition(0);
            } else {
                dataList.addAll(bean.getProductList());
            }
            mAdapter.refreshData(typeImgList, dataList);
        }
    }

    @Override
    public void onRefresh() {//刷新
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                refreshLayout.finishRefreshing();
//            }
//        }, 1500);
        refreshFlag = true;
        page = 0;
//        dataList.clear();
        if (null != getArguments()) {
            mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), page, pageSize, sort, type);
        } else {
            mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", page, pageSize, sort, type);
        }
    }

    @Override
    public void loadingFinished() {
        super.loadingFinished();
        if(refreshFlag)
            refreshLayout.finishRefreshing();
        else
            refreshLayout.finishLoadmore();
//        if(pd!=null&&pd.isShowing()){
//            pd.dismiss();
//        }
    }

    @Override
    public void onLoad() {//加载
        refreshFlag = false;
        page = dataList.size();
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                refreshLayout.finishLoadmore();
//            }
//        }, 1500);
        if (null != getArguments()) {
            mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), page, pageSize, sort, type);
        } else {
            mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", page, pageSize, sort, type);
        }
    }

    @OnClick({R.id.rb_big_img, R.id.rb_list, R.id.layout_tuijian, R.id.layout_qitou, R.id.layout_qixian, R.id.layout_shouyi, R.id.layout_yongjin,R.id.rl_search_by_conditions})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_big_img:
                //设置大图展示样式
                AnimalUtil.QTanEndAnimal(rbBigImg, 200);
                rbBigImg.setTextColor(getResources().getColor(R.color.white));
                rbList.setTextColor(getResources().getColor(R.color.title_select_color));
                typeImgList = 0;
                mAdapter.refreshData(typeImgList, dataList);
                break;
            case R.id.rb_list:
                //设置列表展示样式
                AnimalUtil.QTanEndAnimal(rbList, 200);
                rbBigImg.setTextColor(getResources().getColor(R.color.title_select_color));
                rbList.setTextColor(getResources().getColor(R.color.white));
                typeImgList = 1;
                mAdapter.refreshData(typeImgList, dataList);
                break;
            case R.id.layout_tuijian:
                if(pd!=null)
                    pd.show();
                textQitou.setTextColor(Color.parseColor("#FFFFFF"));
                layoutTuijian.setTextColor(getResources().getColor(R.color.title_select_color));
                textQixian.setTextColor(Color.parseColor("#FFFFFF"));
                textShouyi.setTextColor(Color.parseColor("#FFFFFF"));
                textYongjin.setTextColor(Color.parseColor("#FFFFFF"));
                refreshFlag = true;
                sort = 0;
                if (0 == flag1) {
                    flag1 = 1;
                }
                if (1 == flag1) {
                    flag2 = 0;
                    flag3 = 0;
                    flag4 = 0;
                    flag5 = 0;
                    layoutTuijian.setTextColor(getResources().getColor(R.color.title_select_color));
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else {
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                }
                if (null == titleBean) {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", 0, pageSize, sort, type);
                } else {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), 0, pageSize, sort, type);
                }
                break;
            case R.id.layout_qitou:
                if(pd!=null)
                    pd.show();
                textQitou.setTextColor(getResources().getColor(R.color.title_select_color));
                layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                textQixian.setTextColor(Color.parseColor("#FFFFFF"));
                textShouyi.setTextColor(Color.parseColor("#FFFFFF"));
                textYongjin.setTextColor(Color.parseColor("#FFFFFF"));
                refreshFlag = true;
                sort = 3;
                if (0 == flag2) {
                    flag2 = 1;
                } else if (1 == flag2) {
                    flag2 = 2;
                } else if (2 == flag2) {
                    flag2 = 1;
                }
                if (1 == flag2) {
                    type = 0;
                    flag1 = 0;
                    flag3 = 0;
                    flag4 = 0;
                    flag5 = 0;
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_red);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else if (2 == flag2) {
//                    type = 0;
                    type = 1;
                    flag1 = 0;
                    flag3 = 0;
                    flag4 = 0;
                    flag5 = 0;
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_uparrows_red);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else {
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                }
                if (null == titleBean) {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", 0, pageSize, sort, type);
                } else {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), 0, pageSize, sort, type);
                }
                break;
            case R.id.layout_qixian:
                if(pd!=null)
                    pd.show();
                textQitou.setTextColor(Color.parseColor("#FFFFFF"));
                layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                textQixian.setTextColor(getResources().getColor(R.color.title_select_color));
                textShouyi.setTextColor(Color.parseColor("#FFFFFF"));
                textYongjin.setTextColor(Color.parseColor("#FFFFFF"));
                refreshFlag = true;
                sort = 4;
                if (0 == flag3) {
                    flag3 = 1;
                } else if (1 == flag3) {
                    flag3 = 2;
                } else if (2 == flag3) {
                    flag3 = 1;
                }
                if (1 == flag3) {
                    type = 0;
                    flag1 = 0;
                    flag2 = 0;
                    flag4 = 0;
                    flag5 = 0;
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_red);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else if (2 == flag3) {
                    type = 1;
//                    type = 0;
                    flag1 = 0;
                    flag3 = 0;
                    flag4 = 0;
                    flag5 = 0;
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_uparrows_red);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else {
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                }
                if (null == titleBean) {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", 0, pageSize, sort, type);
                } else {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), 0, pageSize, sort, type);
                }
                break;
            case R.id.layout_shouyi:
                if(pd!=null)
                    pd.show();
                textQitou.setTextColor(Color.parseColor("#FFFFFF"));
                layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                textQixian.setTextColor(Color.parseColor("#FFFFFF"));
                textShouyi.setTextColor(getResources().getColor(R.color.title_select_color));
                textYongjin.setTextColor(Color.parseColor("#FFFFFF"));
                refreshFlag = true;
                sort = 1;
                if (0 == flag4) {
                    flag4 = 1;
                } else if (1 == flag4) {
                    flag4 = 2;
                } else if (2 == flag4) {
                    flag4 = 1;
                }
                if (1 == flag4) {
                    type = 0;
                    flag1 = 0;
                    flag2 = 0;
                    flag3 = 0;
                    flag5 = 0;
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_red);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else if (2 == flag4) {
                    type = 1;
//                    type = 0;
                    flag1 = 0;
                    flag3 = 0;
                    flag2 = 0;
                    flag5 = 0;
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_uparrows_red);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                } else {
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                }
                if (null == titleBean) {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", 0, pageSize, sort, type);
                } else {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), 0, pageSize, sort, type);
                }
                break;
            // 期限
            case R.id.layout_yongjin:
                if(pd!=null)
                    pd.show();
                textQitou.setTextColor(Color.parseColor("#FFFFFF"));
                layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                textQixian.setTextColor(Color.parseColor("#FFFFFF"));
                textShouyi.setTextColor(Color.parseColor("#FFFFFF"));
                textYongjin.setTextColor(getResources().getColor(R.color.title_select_color));
                refreshFlag = true;
                sort = 2;
                if (0 == flag5) {
                    flag5 = 1;
                } else if (1 == flag5) {
                    flag5 = 2;
                } else if (2 == flag5) {
                    flag5 = 1;
                }
                if (1 == flag5) {
                    type = 0;
                    flag1 = 0;
                    flag2 = 0;
                    flag3 = 0;
                    flag4 = 0;
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_arrows_red);
                } else if (2 == flag5) {
                    type = 1;
//                    type = 0;
                    flag1 = 0;
                    flag3 = 0;
                    flag2 = 0;
                    flag4 = 0;
                    layoutQitouIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutQixianIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutShouyiIcon.setBackgroundResource(R.mipmap.home_arrows_gray);
                    layoutYongjinIcon.setBackgroundResource(R.mipmap.home_uparrows_red);
                } else {
                    layoutTuijian.setTextColor(Color.parseColor("#FFFFFF"));
                }
                if (null == titleBean) {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),"", 0, pageSize, sort, type);
                } else {
                    mPresenter.loadProductList(AppUtil.getUserId(getActivity()),titleBean.getId(), 0, pageSize, sort, type);
                }
                break;
            // 筛选条件弹出
            case R.id.rl_search_by_conditions:
//                HttpManager.getInstance().searchFilter().compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterestLabelBean>() {
//                    @Override
//                    public void onNext(InterestLabelBean o) {
//                        ((MainActivity) getActivity()).showSearchPop(o.getList());
//                    }
//                }, this));
                if(AppUtil.isExamined(getActivity()))
                    startActivity(new Intent(getActivity(), SearchByConditionsActivity.class));
                break;
        }
    }

    @Override
    public void loadProductTypeSuccess(ProductTypeListBean productTypeListBean) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }

    @Override
    public void showMsg(String msg) {
        super.showMsg(msg);
        refreshLayout.finishRefreshing();
        refreshLayout.finishLoadmore();
        refreshLayout1.finishRefreshing();
        refreshLayout1.finishLoadmore();
    }
}